$(function(){
	var $hrf = window.location
	var $str = decodeURIComponent($hrf);
	var $dd = $str.split("?");
	var $main = $dd[1];
	var $img = $(".small-imgs li img:first")
	$.getJSON("../jsons/goodlist.json", function(json){
		console.log($main)
		$.each($img,function(i){
			$img.attr("data-id",$main)
		})
		var $imgsrc = json[$main].imgsrc;
		$(".big-imgs img").attr("src", ("../detail-img/" +$imgsrc[0]))
		console.log($imgsrc)
		$.each($imgsrc, function(i){
			$("<li><img src= '../detail-img/"+$imgsrc[i]+"'/></li>").appendTo($(".small-imgs"))
		})
		
		$('<P class="name">'+json[$main].p2 +'</p><p class="xielie">'+ json[$main].p3 +'</p><p data-good-id="'+ $main +'" class="huohao">货号： '+json[$main].huohao+ '</p>').appendTo($("#message"));
		$('<span>￥</span><b> '+json[$main].price+'</b> <del>市场价： ￥ 2800</del>').appendTo($(".priced .price"))
		
	})
	//购买
	var $buy = function(e){
		var $href = window.location
		var $str = decodeURIComponent($href);
		var $dd = $str.split("?")
		var $main = $dd[1]
		var goodId = $(".huohao").attr("data-good-id");
		var goodName = $(".name").html();
		var goodPrice = parseFloat($(".price b").html())
		console.log(goodPrice)
		var goodSrc = $(".small-imgs li:first img").attr("src")
		var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
		var cartObj = convertCartStrToObj(cartStr);
		if(goodId in cartObj){
			cartObj[goodId].num += 1;
		}else{
			cartObj[goodId] = {
				name: goodName,
				price: goodPrice,
				num: 1,
				src: goodSrc
			}
		}
		cartStr = convertObjToCartStr(cartObj);
		console.log(cartStr)
		$.cookie("cart",cartStr,{expires: 7,path: "/"})
		
	}
	//立即购买
	
	$("#buynow").click(function(){
		$buy()
		location.href = "cart.html"
	});
	//加入购物车
	$("#pushcart").click($buy)
	function convertCartStrToObj(cartStr){
				if(!cartStr){
					return {};
				}
				var goods = cartStr.split(":");
				var obj = {};
				for(var i = 0; i < goods.length; i ++){
					var data = goods[i].split(",");
					obj[data[0]] = {
						name : data[1],
						price : parseFloat(data[2]),
						num : parseInt(data[3]),
						src : data[4]
					}
				}
				return obj;
			}
			function convertObjToCartStr(obj){
					
					var cartStr = "";
					//遍历对象
					for(var id in obj){
						if(cartStr){
							cartStr += ":";
						}
						cartStr += id + "," + obj[id].name + "," + obj[id].price + "," + obj[id].num + "," + obj[id].src;
					}
					return cartStr;
			}
		
		//加载购物车中的信息
		function loadCart(){
			var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
			var cartObj = convertCartStrToObj(cartStr);
			var total = 0;
			for(var id in cartObj){
				total +=cartObj[id].num;
			}
			$(".cart-num").val(total);
			
		}
	
	
	
	
	
	//放大镜
	$(".small-imgs").on("click" , "li",(function(){
		var i = $(this).index();
		console.log(i);
		var smallimgsrc = $(this).find("img").attr("src");
		//console.log(smallimgsrc);
		$(".big-imgs img").attr("src",smallimgsrc)
	}))
//	$(".small-imgs li").click(function(){
//		var i = $(this).index();
//		var smallimgsrc = $(this).find("img").attr("src");
//		//console.log(smallimgsrc);
//		$(".big-imgs img").attr("src",smallimgsrc)
//	})
	$(".big-imgs").hover(function(){
		//console.log($(".big-imgs img").attr("src"))
		$("<div class='bigkuang'><img src='"+ $('.big-imgs img').attr('src') +"'/></div>").appendTo($(".goods-left"));
		$("<div class='smallkuang'></div>").appendTo($(".big-imgs"))
		var scale = 2;
		$(this).mousemove(function(e){
			$(".bigkuang").animate({"height": "480px","width": "480px","left": "500px","top": "0"})
			var _left = e.clientX - $(".big-imgs").offset().left - $(".smallkuang").width()/2;
			var _top = e.clientY - $(".big-imgs").offset().top - $(".smallkuang").height()/2 +$(document.body).scrollTop();
			
			_left = Math.min( Math.max(0 , _left) ,($(".big-imgs").width()-$(".smallkuang").width()) );
			_top = Math.min( Math.max(0 , _top) ,($(".big-imgs").width()-$(".smallkuang").width()) );
			$(".smallkuang").css("left",_left);
			$(".smallkuang").css("top",_top);
			$(".bigkuang img").css({"left": -_left*scale,"top":-_top*scale})
		})
	},function(){
		$(".bigkuang").remove();
		$(".smallkuang").remove();
	})
	//顶部悬浮
	$(window).scroll(function(){
		
		if($(this).scrollTop() >= 900){
			$(".main-left-title").css({"position": "fixed","top":"0","left":"82px","padding":"13px 74px"})
			$(".main-right-title").css({"position": "fixed","top":"0","left":"277px"})
			$(".main-right-title li").css("padding","13px 99px")
		}else{
			$(".main-left-title").css({"position": "absolute","padding":"13px 63px"})
			$(".main-right-title").css({"position": "absolute","left":"309px"})
			$(".main-right-title li").css({"padding":"13px 63px"})
			$("")
		}
	})
	//三个遮罩层
	$(".main-right-img1").hover(function(){
		$(".bg-top").eq(0).show();
	},function(){
		$(".bg-top").eq(0).hide();
	})
	$(".main-right-img2 dt").hover(function(){
		console.log(($(this).index()) + 1)
		$(".main-right-img2 .bg-top").eq(0).show();
	},function(){
		$(".main-right-img2 .bg-top").eq(0).hide();
	})
	$(".main-right-img2 dd").hover(function(){
		console.log(($(this).index()) + 1)
		$(".main-right-img2 .bg-top").eq(1).show();
	},function(){
		$(".main-right-img2 .bg-top").eq(1).hide();
	})
	//main-right-title选项卡
	$(".main-right-title li").click(function(){
		var i = $(this).index();
		$(document.body).scrollTop(1500);
		$(".main-detail ul li").hide();
		$(".main-detail ul li").eq(i).show();
	})
	//top
	$(".top-myfifth").hover(function(){
		$(".myfifth").css("display","block")
	},function(){
		$(".myfifth").css("display","none")
	});
	//购物车小图标跳转cart.html
    $(".top-bottom-right").click(function(){
		location.href= "html/cart.html"
	})
    var num = getCookie("num") ? getCookie("num") : 0;
   $(".cart-num").html(num)
})
